<template>
  <div class="classify">
    <div class="classify_head">
      <div class="classify_head_left">请选择店铺主营类目(多选)</div>
      <div class="classify_head_right" @click="setClass">确定</div>
    </div>
    <div class="classify_con">
      <div class="classify_list" v-for="(item,index) in list" @click="addClass(index)">
        <div class="classify_list_left">{{item.goods_category_name_str}}</div>
        <div class="classify_list_right">
          <span>{{number}}</span>
          <img v-if="item.state" src="../../../static/image/someSelect_sure.png" alt>
          <img v-else src="../../../static/image/someSelect_defaul.png" alt>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: ["store_main_product"],
  data() {
    return {
      list: [],
      number: 0
    };
  },
  mounted() {
    this.get_main_product_list();
  },
  methods: {
    // 设置分类
    setClassstate() {
      let li = this.store_main_product.split(",");
      for (let i in li) {
        for (let j in this.list) {
          if (this.list[j].goods_category_id == li[i]) {
            this.list[j].state = true;
          }
        }
      }
    },
    // 设置主营类目
    store_main_product_edit_post(list,cb) {
      this.appJS.ajax("user.store_main_product_edit_post", "post", {
        store_main_product:list.join(',')
      }, res => {
        cb(res.data.store_id)
      });
    },
    // 获取分类列表
    get_main_product_list() {
      this.appJS.ajax("user.get_main_product_list", "post", {}, res => {
        for (var i in res.data.main_product_list) {
          res.data.main_product_list[i].state = false;
        }
        this.list = res.data.main_product_list;
        this.setClassstate();
      });
    },
    //   添加分类
    addClass(index) {
      this.number += 1;
      this.list[index].state = !this.list[index].state;
      console.log(this.list[index]);
    },
    // 确定分类
    setClass() {
      let list = [];
      for (var i in this.list) {
        if (this.list[i].state) {
          list.push(this.list[i].goods_category_id);
        }
      }
      this.store_main_product_edit_post(list, store_id => {
        console.log(store_id);
        this.getShopData(store_id,()=>{
          this.$emit("setClass");
        })
      });
    },
    // 获取店铺信息
    getShopData(store_id,cb) {
      this.appJS.ajax(
        "user.get_store_info",
        "post",
        {
          store_id: store_id
        },
        res => {
          console.log(res.data);
          this.appJS.setStorage('store_info',res.data.store_info);
          cb()
        }
      );
    },
  },
  components: {}
};
</script>

<style lang='less'>
.classify {
  height: calc(100vh - 57px);
  background: #fff;
  width: 100%;
  overflow-y: auto;
  position: relative;
  &::-webkit-scrollbar {
    display: none;
  }
  .classify_head {
    height: 3rem;
    width: 100%;
    padding: 0 0.5rem;
    font-size: 0.9rem;
    color: #fff;
    background: @color;
    position: fixed;
    top: 0;
    left: 0;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    .classify_head_left {
      height: 100%;
      width: 80%;
      display: flex;
      justify-content: flex-start;
      align-items: center;
    }
    .classify_head_right {
      display: flex;
      justify-content: flex-end;
      align-items: center;
      width: 20%;
      height: 100%;
      padding-right: 0.5rem;
    }
  }
  .classify_con {
    padding-top: 3rem;
  }
  .classify_list {
    height: 2.5rem;
    width: 100%;
    padding: 0 0.5rem;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    font-size: 0.8rem;
    border-bottom: 1px solid #efefef;
    transition: 0.4s;
    &:active {
      background: #efefef;
    }
    .classify_list_left {
      height: 100%;
      width: 80%;
      display: flex;
      justify-content: flex-start;
      align-items: center;
    }
    .classify_list_right {
      padding-right: 0.5rem;
      height: 100%;
      width: 20%;
      display: flex;
      justify-content: flex-end;
      align-items: center;
      span {
        font-size: 0.4rem;
        color: #fff;
        opacity: 0;
      }
      img {
        height: 1rem;
        width: 1rem;
      }
    }
    &:last-child {
      border: 0;
    }
  }
}
</style>
